package Server;

import Data.*;
import MyInterfaces.MassageType;
import Search.SearchGroup;
import jdk.net.Sockets;

import java.io.*;
import java.net.Socket;
import java.nio.file.Files;
import java.sql.*;
import java.util.ArrayList;

public class GroupOperation {
    private GroupData groupData;
    String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    String user = "root";
    String password = "Mi20030610.";

    public GroupData getGroupData() {
        return groupData;
    }

    public void setGroupData(GroupData groupData) {
        this.groupData = groupData;
    }

    public GroupOperation(GroupData groupData) {
        this.groupData = groupData;
    }

    public GroupOperation() {
    }

    public boolean addToGroup(GroupOperationData groupOperationData) throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        GroupFriendData receiver=groupOperationData.getReceiver();
        //先写入数据库，给邀请人回复一条数据
        String sql="insert into group_identity_list (id,identity,group_id) values (?,3,?);";
        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,receiver.getId());
        operation.setString(2,receiver.getGroup_id());

        int k=operation.executeUpdate();

        String sender=groupOperationData.getSender();
        ArrayList<GroupFriendData> arrayList=groupOperationData.getFriendlist();
        receiver.setIdentity(3);
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getId().equals(sender))
            {
                //如果是自己
                event returnmassage=new event();
                returnmassage.setType(MassageType.Return_Invite_Other_To_Group_Success);
                returnmassage.setT(receiver);
                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(sender);
                if(socket!=null)
                {
                    System.out.println("给自己发送消息");
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
            }
            else
            {
                if(ManageClientConnectionServerThread.existId(arrayList.get(i).getId()))
                {
                    System.out.println(arrayList.get(i).getId()+"在线，快通知ta");
                    event returnmassage=new event();
                    returnmassage.setType(MassageType.Add_Group_People);
                    returnmassage.setT(receiver);
                    Socket socket=ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getId());
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
            }
        }

        //从数据库知道群聊的消息
        String newsql="select *from chat_room_list where group_id=?;";
        operation=Conn.prepareStatement(newsql);
        operation.setString(1,receiver.getGroup_id());

        ResultSet resultSet=operation.executeQuery();
        if(resultSet.next())
        {
            GroupData newgroupdata = new GroupData();
            newgroupdata.setIdentity(3);
            newgroupdata.setId(receiver.getId());
            newgroupdata.setGroup_id(receiver.getGroup_id());
            newgroupdata.setGroup_name(resultSet.getString("name"));
            newgroupdata.setGroup_saying(resultSet.getString("group_saying"));

            String path = resultSet.getString("group_avatar");
            byte[] t = null;
            System.out.println(path);
            if (path != null) {
                File file = new File(path);

                if (file.exists()) {
                    System.out.println("头像数据不为空");
                    byte[] buffer = new byte[1024 * 5];
                    t = new byte[(int) file.length()];
                    InputStream is = Files.newInputStream(file.toPath());
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    int len;
                    while ((len = is.read(buffer)) != -1) {
                        bos.write(buffer, 0, len);
                    }
                    t = bos.toByteArray();
                }
            }
            newgroupdata.setGroup_avatar(t);

            Socket socket=ManageClientConnectionServerThread.getSocketByUserid(receiver.getId());
            if(socket!=null)
            {
                event returnmassage=new event();
                returnmassage.setType(MassageType.Add_Group);
                returnmassage.setT(newgroupdata);
                ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                writer.writeObject(returnmassage);
                writer.flush();;
            }
            System.out.println("设置群聊信息给被邀请的人啦");
            return true;
        }
       return false;
    //    operation.setString(2,groupFriendData.);
    }

    public boolean delInGroup(GroupOperationData groupOperationData) throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        //删掉这个人
        String sql="delete from group_identity_list where id=? and group_id=?;";
        GroupFriendData receiver=groupOperationData.getReceiver();
        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,receiver.getId());
        operation.setString(2,receiver.getGroup_id());

        int k=operation.executeUpdate();
        if(k<=0) return false;

        ArrayList<GroupFriendData> arrayList=groupOperationData.getFriendlist();
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getId().equals(receiver.getId()))
            {
                //如果是自己需要直接退出群聊
                event returnmassage=new event();
                returnmassage.setType(MassageType.Delete_Group);
                GroupData newdata=new GroupData();
                newdata.setId(receiver.getId());
                newdata.setGroup_id(receiver.getGroup_id());
                returnmassage.setT(newdata);

                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(receiver.getId());
                if(socket!=null)
                {
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
            }
            else if(ManageClientConnectionServerThread.existId(arrayList.get(i).getId()))
            {
                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getId());

                event returnmassage=new event();
                returnmassage.setType(MassageType.Return_Delete_Group_People_Success);
                returnmassage.setT(receiver);

                ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                writer.writeObject(returnmassage);
                writer.flush();
            }

        }
        return true;


//        if(k>0)
//        {
//            //删除好友之后需要刷新其他人的好友列表
//            ArrayList<GroupFriendData> arrayList=groupData.getGroup_friend_list();
//            for (int i = 0; i < arrayList.size(); i++)
//            {
//                if(arrayList.get(i).getId().equals(groupData.getId()))
//                {
//                    //如果是被删掉的人，需要直接删掉该群
//                    event returnmassage=new event();
//                    returnmassage.setType(MassageType.Return_Delete_Group_Success);
//                    returnmassage.setT(groupData);
//
//                    ObjectOutputStream writer=new ObjectOutputStream(ManageClientConnectionServerThread.getSocketByUserid(groupData.getId()).getOutputStream());
//                    writer.writeObject(returnmassage);
//                    writer.flush();
//                }
//                else if(ManageClientConnectionServerThread.existId(arrayList.get(i).getId()))
//                {
//                    //如果在线
//                    Socket socket=ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getId());
//                    GroupFriendData groupFriendData=new GroupFriendData();
//                    groupFriendData.setId(groupData.getId());
//                    groupFriendData.setGroup_id(groupData.getGroup_id());
//                    groupFriendData.setIdentity(groupData.getIdentity());
//
//                    event returnmassage=new event();
//                    returnmassage.setType(MassageType.
//                            );
//                    returnmassage.setT(groupFriendData);
//                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
//                    writer.writeObject(returnmassage);
//                    writer.flush();
//                }
//            }
//            return true;
//        }
//        else return false;

    }

    public boolean changeAdmin(GroupOperationData groupOperationData) throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        //变成管理员
        GroupFriendData receiver=(GroupFriendData)groupOperationData.getReceiver();
        String sql="update group_identity_list set identity=2 where id=? and group_id=?";
        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,receiver.getId());
        operation.setString(2,receiver.getGroup_id());

        //修改身份
        int k=operation.executeUpdate();
        if(k<=0) return false;
        System.out.println("数据库写入成功！");

        ArrayList<GroupFriendData> arrayList=groupOperationData.getFriendlist();
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getId().equals(receiver.getId()))
            {
                //此时是接收者，接收者需要改变自己的identity和好友列表
                ChangeGroupFriendListData changedata=new ChangeGroupFriendListData();
                changedata.setGroup_id(receiver.getGroup_id());
                changedata.setId(receiver.getId());
                changedata.setIdentity(2);

                //ta的好友列表不需要变，因为只需要修改自己就可以了
                changedata.setArrayList(null);

                event returnmassage=new event();
                returnmassage.setType(MassageType.Change_GroupData);
                returnmassage.setT(changedata);
                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(receiver.getId());
                if(socket!=null)
                {
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
                System.out.println("成功改写"+receiver.getId()+"的身份");
            }
            else if(ManageClientConnectionServerThread.existId(arrayList.get(i).getId()))
            {
                //在线，需要修改好友列表的数据
                System.out.println(arrayList.get(i).getId()+"ta在线,改变ta的好友列表显示");
                ChangeGroupFriendListData changedata=new ChangeGroupFriendListData();
                changedata.setGroup_id(receiver.getGroup_id());
                changedata.setId(arrayList.get(i).getId());

                GroupFriendData groupFriendData=new GroupFriendData();
                groupFriendData.setIdentity(2);
                groupFriendData.setId(receiver.getId());
                groupFriendData.setGroup_id(receiver.getGroup_id());

                ArrayList<GroupFriendData> arry=new ArrayList<>();
                arry.add(groupFriendData);
                changedata.setArrayList(arry);

                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getId());
                if(socket!=null)
                {
                    event returnmassage=new event();
                    returnmassage.setType(MassageType.Change_Group_Friend_List);
                    returnmassage.setT(changedata);

                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
            }
        }
        return true;
    }

    public boolean changeOwner(GroupOperationData groupOperationData) throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        String sql="update group_identity_list set identity=? where id=? and group_id=?;";

        String sender=groupOperationData.getSender();
        GroupFriendData receiver=groupOperationData.getReceiver();
        ArrayList<GroupFriendData> arrayList=groupOperationData.getFriendlist();
        PreparedStatement operation=Conn.prepareStatement(sql);
        //先把群主设为普通人
        operation.setInt(1,3);
        operation.setString(2,sender);
        operation.setString(3,receiver.getGroup_id());

        int k=operation.executeUpdate();
        if(k<=0) return false;
        else System.out.println("设置群主为普通人成功");

        //把另外一个人设置成群主
        operation=Conn.prepareStatement(sql);
        operation.setInt(1,1);
        operation.setString(2,receiver.getId());
        operation.setString(3,receiver.getGroup_id());

        k=operation.executeUpdate();
        if(k<=0) return false;
        else System.out.println("设置普通人为群主成功");

        //修改chat_room_list的群主id
        String newsql="update chat_room_list set creater_id=? where group_id=?;";
        operation=Conn.prepareStatement(newsql);
        operation.setString(1,receiver.getId());
        operation.setString(2,receiver.getGroup_id());

        k=operation.executeUpdate();
        if(k<=0) return false;
        else System.out.println("chat_room_list设置群主成功");

        //给在线列表发送消息，需要修改的消息是俩条，所有人应该都是，sender代表群主，receiver代表非群主
        // 找到相应的id，更改即可，所有人都返回一个change_Group_Friend_List的消息即可
        //除了这俩个人，其他只需要刷新，这俩人需要重画窗口，除了聊天记录不重新画，
        groupOperationData.setFriendlist(null);
        receiver.setIdentity(1);
        System.out.println("列表人数为"+arrayList.size());
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getId().equals(sender))
            {
                System.out.println("改变群主的身份");
                //如果这个人是之前的群主，需要修改的是，群聊identity和好友identity，然后刷新列表
                event returnmassage=new event();
                returnmassage.setType(MassageType.Change_GroupData);

                //需要修改的好友列表
                ArrayList<GroupFriendData> arry=new ArrayList<>();
                arry.add(receiver);

                //然后还有就是自己的标识需要修改，其实修改了自己对应的标识，那么好友列表需要修改的标识就只有一个了
                ChangeGroupFriendListData changedata=new ChangeGroupFriendListData();
                changedata.setGroup_id(receiver.getGroup_id());
                changedata.setIdentity(3);
                changedata.setId(sender);
                changedata.setArrayList(arry);

                returnmassage.setT(changedata);

                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(sender);
                if(socket!=null)
                {
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }

            }
            else if(arrayList.get(i).getId().equals(receiver.getId()))
            {
                System.out.println(receiver.getId()+"修改成群主");
                //这个是后来的群主，也需要修改自己的identity和好友的信息，然后刷新列表
                event returnmassage=new event();
                returnmassage.setType(MassageType.Change_GroupData);

                //然后还有就是自己的标识需要修改，其实修改了自己对应的标识，那么好友列表需要修改的标识就只有一个了
                ChangeGroupFriendListData changedata=new ChangeGroupFriendListData();
                changedata.setGroup_id(receiver.getGroup_id());
                changedata.setIdentity(1);
                changedata.setId(receiver.getId());

                //需要修改的好友列表,此时被设为群主的人需要修改
                //自己好友列表当中设为群众的人的id
                ArrayList<GroupFriendData> arry=new ArrayList<>();
                GroupFriendData groupFriendData=new GroupFriendData();
                groupFriendData.setIdentity(3);
                groupFriendData.setId(sender);
                groupFriendData.setGroup_id(receiver.getGroup_id());
                arry.add(groupFriendData);
                changedata.setArrayList(arry);

                returnmassage.setT(changedata);

                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(receiver.getId());
                if(socket!=null)
                {
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
            }
            else if(ManageClientConnectionServerThread.existId(arrayList.get(i).getId()))
            {
                System.out.println(arrayList.get(i).getId()+"ta在线，让ta刷新列表");
                //这里需要刷新列表
                event returnmassage=new event();
                returnmassage.setType(MassageType.Change_Group_Friend_List);
                //修改列表，俩个groupfrienddata

                ArrayList<GroupFriendData> arry=new ArrayList<>();

                //之前的群主修改成现在的群友3号
                GroupFriendData first=new GroupFriendData();
                first.setId(sender);
                first.setGroup_id(receiver.getGroup_id());
                first.setIdentity(3);

                arry.add(first);
                arry.add(receiver);

                ChangeGroupFriendListData changedata=new ChangeGroupFriendListData();
                changedata.setGroup_id(receiver.getGroup_id());
                changedata.setArrayList(arry);

                returnmassage.setT(changedata);

                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getId());
                if(socket!=null)
                {
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
            }
        }
        System.out.println("一切设置完毕");
        return true;
    }

    public boolean getRidAdmin(GroupOperationData groupOperationData) throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        //变成管理员
        GroupFriendData receiver=(GroupFriendData)groupOperationData.getReceiver();
        String sql="update group_identity_list set identity=3 where id=? and group_id=?";
        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,receiver.getId());
        operation.setString(2,receiver.getGroup_id());

        //修改身份
        int k=operation.executeUpdate();
        if(k<=0) return false;
        System.out.println("数据库写入成功！");

        ArrayList<GroupFriendData> arrayList=groupOperationData.getFriendlist();
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getId().equals(receiver.getId()))
            {
                //此时是接收者，接收者需要改变自己的identity和好友列表
                ChangeGroupFriendListData changedata=new ChangeGroupFriendListData();
                changedata.setGroup_id(receiver.getGroup_id());
                changedata.setId(receiver.getId());
                changedata.setIdentity(3);

                //ta的好友列表不需要变，因为只需要修改自己就可以了
                changedata.setArrayList(null);

                event returnmassage=new event();
                returnmassage.setType(MassageType.Change_GroupData);
                returnmassage.setT(changedata);
                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(receiver.getId());
                if(socket!=null)
                {
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
                System.out.println("成功改写"+receiver.getId()+"的身份");
            }
            else if(ManageClientConnectionServerThread.existId(arrayList.get(i).getId()))
            {
                //在线，需要修改好友列表的数据
                System.out.println(arrayList.get(i).getId()+"ta在线,改变ta的好友列表显示");
                ChangeGroupFriendListData changedata=new ChangeGroupFriendListData();
                changedata.setGroup_id(receiver.getGroup_id());
                changedata.setId(arrayList.get(i).getId());

                GroupFriendData groupFriendData=new GroupFriendData();
                groupFriendData.setIdentity(3);
                groupFriendData.setId(receiver.getId());
                groupFriendData.setGroup_id(receiver.getGroup_id());

                ArrayList<GroupFriendData> arry=new ArrayList<>();
                arry.add(groupFriendData);
                changedata.setArrayList(arry);

                Socket socket=ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getId());
                if(socket!=null)
                {
                    event returnmassage=new event();
                    returnmassage.setType(MassageType.Change_Group_Friend_List);
                    returnmassage.setT(changedata);

                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
            }
        }
        return true;
    }

    public boolean delGroup() throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        String sql="delete from chat_room_list where group_id=?;";
        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,groupData.getGroup_id());

        int k=operation.executeUpdate();
        System.out.println("删除群聊的信息完毕");
        if(k>0)
        {
            String finalsql="delete from group_identity_list where group_id=?;";
            operation=Conn.prepareStatement(finalsql);
            operation.setString(1,groupData.getGroup_id());
            int  y=operation.executeUpdate();
            System.out.println("删除所有在群聊里面的人的身份信息完毕");

            String newsql="delete from massagelist where receiver_id=?;";
            //删除群聊聊天记录
            operation=Conn.prepareStatement(newsql);
            operation=Conn.prepareStatement(newsql);
            operation.setString(1,groupData.getGroup_id());

            int j=operation.executeUpdate();
            System.out.println("删除群聊的所有聊天记录");
            //让其他在线的人刷新列表
            ArrayList<GroupFriendData> arrayList=groupData.getGroup_friend_list();
            groupData.setGroup_friend_list(null);
            for (int i = 0; i < arrayList.size(); i++) {
                if(arrayList.get(i).getId().equals(groupData.getId()))
                {

                }
                else if(ManageClientConnectionServerThread.existId(arrayList.get(i).getId()))
                {
                    System.out.println("ta在线，需要提醒ta"+arrayList.get(i).getId()+"删除");
                    Socket friend=ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getId());
                    ObjectOutputStream writer=new ObjectOutputStream(friend.getOutputStream());
                    event returnmassage=new event();
                    returnmassage.setType(MassageType.Delete_Group);
                    returnmassage.setT(groupData);
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
            }
        }
        operation.close();
        Conn.close();
        return true;
    }

    public boolean AgreeJoinInGroup(GroupOperationData groupOperationData) throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        GroupFriendData receiver=groupOperationData.getReceiver();
        System.out.println(receiver.getId()+"----"+receiver.getGroup_id());
        //先写入数据库，给邀请人回复一条数据
        String sql="update group_identity_list set identity=3 where id=? and group_id=?;";
        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,receiver.getId());
        operation.setString(2,receiver.getGroup_id());

        int k=operation.executeUpdate();
        System.out.println("更新数据库成功");

        ArrayList<GroupFriendData> arrayList=groupOperationData.getFriendlist();
        receiver.setIdentity(3);
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getId().equals(receiver.getId()))
            {
                //如果是自己,从数据库知道群聊的消息
                String newsql="select *from chat_room_list where group_id=?;";
                operation=Conn.prepareStatement(newsql);
                operation.setString(1,receiver.getGroup_id());

                ResultSet resultSet=operation.executeQuery();
                if(resultSet.next())
                {
                    GroupData newgroupdata = new GroupData();
                    newgroupdata.setIdentity(3);
                    newgroupdata.setId(receiver.getId());
                    newgroupdata.setGroup_id(receiver.getGroup_id());
                    newgroupdata.setGroup_name(resultSet.getString("name"));
                    newgroupdata.setGroup_saying(resultSet.getString("group_saying"));

                    String path = resultSet.getString("group_avatar");
                    byte[] t = null;
                    System.out.println(path);
                    if (path != null) {
                        File file = new File(path);

                        if (file.exists()) {
                            System.out.println("头像数据不为空");
                            byte[] buffer = new byte[1024 * 5];
                            t = new byte[(int) file.length()];
                            InputStream is = Files.newInputStream(file.toPath());
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            int len;
                            while ((len = is.read(buffer)) != -1) {
                                bos.write(buffer, 0, len);
                            }
                            t = bos.toByteArray();
                        }
                    }
                    newgroupdata.setGroup_avatar(t);

                    Socket socket=ManageClientConnectionServerThread.getSocketByUserid(receiver.getId());
                    if(socket!=null)
                    {
                        event returnmassage=new event();
                        returnmassage.setType(MassageType.Add_Group);
                        returnmassage.setT(newgroupdata);
                        ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();;
                        System.out.println("给自己返回一个入群聊的信息");
                    }
                    System.out.println("成功给群聊列表返回数据");
                }
                else return false;
            }
            else
            {
                if(ManageClientConnectionServerThread.existId(arrayList.get(i).getId()))
                {
                    //如果这个人在线，就执行
                    System.out.println(arrayList.get(i).getId()+"在线，快通知ta");
                    event returnmassage=new event();
                    returnmassage.setType(MassageType.Change_Group_Friend_List);

                    ChangeGroupFriendListData changedata=new ChangeGroupFriendListData();
                    ArrayList<GroupFriendData> arry=new ArrayList<>();
                    arry.add(receiver);

                    changedata.setArrayList(arry);
                    returnmassage.setT(changedata);
                    Socket socket=ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getId());
                    ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                    writer.writeObject(returnmassage);
                    writer.flush();
                }
            }
        }
        return true;
    }

    public boolean RefuseJoinInGroup(GroupOperationData groupOperationData) throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);

        GroupFriendData receiver=groupOperationData.getReceiver();
        String sql="delete from group_identity_list where group_id=? and id=?;";
        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,receiver.getGroup_id());
        operation.setString(2,receiver.getId());

        int k=operation.executeUpdate();
        if(k<=0) return false;
        System.out.println("删除数据库身份成功");

        //删除完毕，需要给在线列表返回一个消息
        receiver.setIdentity(3);
        ArrayList<GroupFriendData> arrayList=groupOperationData.getFriendlist();
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getId().equals(receiver.getId()))
            {
                //如果是自己，好像也不需要做啥，毕竟没显示出来
            }
            else
            {
                if(arrayList.get(i).getIdentity()==1||arrayList.get(i).getIdentity()==2)
                {
                    //如果是群主和管理员，需要刷新列表，改变这个人的身份显示
                    if(ManageClientConnectionServerThread.existId(arrayList.get(i).getId()))
                    {
                        //看看他们是否在线,在线需要发送一个更改好友列表的信息,拒绝需要删掉该好友
                        System.out.println("这个人是群主或者管理员"+arrayList.get(i).getId());
                        Socket socket=ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getId());
                        event returnmassage=new event();
                        returnmassage.setType(MassageType.Return_Delete_Group_People_Success);

                        returnmassage.setT(receiver);

                        ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                        writer.writeObject(returnmassage);
                        writer.flush();
                    }
                }
            }
        }
        return true;
    }
}
